【Mybatis】功能强大的动态SQL之if与choose(03) |
您所在的位置:网站首页 › mybatis choose when otherwise › 【Mybatis】功能强大的动态SQL之if与choose(03) |
大家好,我是书架呀。 在前两节的内容中,主要介绍了Mybatis框架依据Dao层接口、映射文件(写SQL语句的Mapper.xml文件)、配置文件SqlMapConfig.xml完成基本的增删改查工作。 这节内容书架将深入映射文件,介绍动态SQL。动态SQL的引入是为了解决复杂业务场景下,更好地控制传入参数对于SQL语句的影响。 项目的运行环境仍然采用【Mybatis】快速搭建01,这里不再赘述。 2.动态SQL-if第一个运用在动态SQL中的法宝是if标签,通常用于where语句中,if主要用来做判断。 通过判断参数值是否满足某个条件来决定是否使用该参数作为查询条件,它也经常用于update语句中判断是否更新某一个字段,还可以在insert语句中用于判断是否插入某个字段的值。 下面逐一介绍上述三个场景。 2.1 查询语句where中使用if 为了更好的理解如何在where中引入if标签,我们先引入一个普通的实例。根据用户的id、username、password查出用户的记录。 Dao层接口UserMapper增加findByCondition方法。 public User findByCondition(User user);映射文件UserMapper.xml中增加 select * from user where id = #{id} and username = #{username} and password = #{password}测试类中的测试代码 @Test public void test6() throws IOException { //1. 读取核心配置文件SqlMapConfig.xml InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2. 创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); //3. 使用工厂生产一个SqlSession对象 SqlSession session = factory.openSession(); //4. 使用SqlSession创建Dao接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setId(6); user.setUsername("caocao"); user.setPassword("hello123"); User byIdTest = userMapper.findByCondition(user); System.out.println(byIdTest); //5. 释放资源 session.close(); in.close(); }在这个示例中,测试方法生成User对象id值为6,username为caocao,password为hello123。 因此在UserMapper.xml的查询语句中这三个属性值都用来作为where后面的查询条件,可以查出数据库中对应的一条记录。 如果将测试方法中的User对象中的password不赋值。继续执行方法查询,发现查询结果为空。 实际上这条查询语句在和数据库中的数据进行匹配时,将password自动匹配成了null,那数据库中肯定没有这行记录。 这个时候如果还想查出数据库中的相应记录,就要采用if来排除为空的字段作为查询条件出现。 上述Dao层接口UserMapper中findByCondition方法不变。 映射文件UserMapper.xml中的select标签修改为 select * from user where 1 = 1 and id = #{id} and username = #{username} and password = #{password}测试方法修改为 @Test public void test6() throws IOException { //1. 读取核心配置文件SqlMapConfig.xml InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2. 创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); //3. 使用工厂生产一个SqlSession对象 SqlSession session = factory.openSession(); //4. 使用SqlSession创建Dao接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setId(6); user.setUsername("caocao"); User byIdTest = userMapper.findByCondition(user); System.out.println(byIdTest); //5. 释放资源 session.close(); in.close(); }在测试方法中创建User对象时,只给id和username属性赋了值。UserMapper.xml采用加入if标签的方式可以查出数据库中的记录。 if标签有个必填的属性test,test的属性值是一个判断表达式,该表达式的结果可以是true或者false,只有为true时,该if标签中的查询条件才会并入where中。 在上述示例中,由于password值为null,根据if子标签的判断为false所以and password = #{password}被排除在了查询条件外,因此可以根据id值和username两个值查询出了数据库中的一条记录。 简而言之,where中if子标签就是对传入参数中的一种筛选,参数符合我设定的条件,我将这个条件放在where后面作为查询条件,不符合条件就不用。 另外要说明的一点是,where后面为啥要跟where 1=1。这是因为如果没有这个条件,当所有if动态判断都不满足条件时,最后生成的SQL就会以where结束,这不符合SQL规范,会报错。 2.2 update更新列中使用if 现在依然通过一个需求来说明这种情况if的用法,实现需求更新数据库中的一条记录时,只更新有变化的字段。 需要注意,更新的时候不能将原来有值但没有发生变化的字段更新为空或null。通过if标签可以实现这种动态更新列。 Dao层接口UserMapper增加updateByIdSelective方法 public int updateByIdSelective(User user);映射文件UserMapper.xml中增加 update user set username = #{username}, password = #{password}, id = #{id} where id = #{id}测试方法修改为 @Test public void test7() throws IOException { //1. 读取核心配置文件SqlMapConfig.xml InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2. 创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); //3. 使用工厂生产一个SqlSession对象 SqlSession session = factory.openSession(); //4. 使用SqlSession创建Dao接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setId(1); user.setUsername("liuchan"); int update = userMapper.updateByIdSelective(user); session.commit(); System.out.println(update); //5. 释放资源 session.close(); in.close(); }在测试方法中我们创建了User对象,给id赋值1,username赋值liuchan,password没有赋值,因此这条更新语句要实现只更新id值为1的username字段。 通过数据库图对比,id号为1的username由zhangsan变化为liuchan。password并没有改变。 试想如果在更新SQL中未使用if作为判断,那么此时password虽然未赋新值会被更新为null。 这里仍需提醒大家注意两点,第一点是每个if标签元素里面SQL语句后面有逗号,第二点是where关键字前面的id={id}这个条件。 下面的两种情况可以帮助大家理解。 第一种情况,如果if判断导致全部条件都为null或者空。如果有id=#{id}这个条件,最终的SQL语句会变成 update user set id = #{id} where id = #{id}。 如果没有这个条件,最终的SQL如下。 update user set where id = #{id}。 这个SQL语法错误,set关键字后面没有内容,直接是where关键字。 第二种情况是查询条件只有一个不是null或者空,假设是username。 如果有id=#{id}这个条件,最终的SQL语句如下。 update user set username = #{username}, id = #{id} where id = #{id} 如果没有这个条件,最终的SQL如下 update user set username = #{username}, where id = #{id} 这样where关键字前面直接是一个逗号,这个SQL语句也是错误的。 简而言之,id=#{id}这个条件可以最大限度保证SQL语法不出错。当然在具体的业务场景中,大家可以根据自己的需要选取充当该角色的字段。 2.3 insert动态插入列中使用if 在数据库表中插入数据的时候,如果某一列的参数值不为空,就使用传入的值,如果传入的值为空,就使用数据库中的默认值(通常是空),而不使用传入的值。 Dao层接口UserMapper增加insert3方法 int insert3(User user);映射文件UserMapper.xml中增加 insert into user ( id, password username ) values ( #{id}, #{password} #{username} )测试方法修改为 @Test public void test8() throws IOException { //1. 读取核心配置文件SqlMapConfig.xml InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2. 创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); //3. 使用工厂生产一个SqlSession对象 SqlSession session = factory.openSession(); //4. 使用SqlSession创建Dao接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setUsername("menghuo"); userMapper.insert3(user); //新增数据需要提交事务 session.commit(); //6. 释放资源 session.close(); in.close(); }测试方法中生成了User类对象,由于id为主键自增,因此不设值,另外只给username赋值。SQL语句中password的值采用if判断,如果不为空,就采用传入的值,如果为空就会使用数据库的默认值。 3.动态SQL-choose 上面介绍了在查询语句时采用if来判断参数是否可用,但是这个只能满足基本的判断,如果想要实现if...else...这样的逻辑,就要用到choose when otherwise标签。 choose这个元素中包含when和otherwise两个标签,一个choose中至少有一个when,有0个或者1个otherwise。 下面通过示例来理解它的用法,假设在user表中除了id值是唯一的,username也是唯一的,那么除了通过id值可以唯一检索到一条记录外,通过username也是可以的。 下面实现如下查询场景,当id这个参数有值的时候就用id值去查询,,当id没有值的时候就去判断用户名是否有值,如果有值就用用户名查询,当判断用户名也没有值的时候,返回空记录。 Dao层接口UserMapper增加selectByIdOrUsername方法 public User selectByIdOrUsername(User user);映射文件UserMapper.xml中增加 select * from user where 1=1 and id = #{id} and username = #{username} and 1=2测试方法修改为 @Test public void test9() throws IOException { //1. 读取核心配置文件SqlMapConfig.xml InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2. 创建SqlSessionFactory工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory factory = builder.build(in); //3. 使用工厂生产一个SqlSession对象 SqlSession session = factory.openSession(); //4. 使用SqlSession创建Dao接口的代理对象 UserMapper userMapper = session.getMapper(UserMapper.class); User user = new User(); user.setUsername("lisi"); User byIdTest = userMapper.selectByIdOrUsername(user); System.out.println(byIdTest); //5. 释放资源 session.close(); in.close(); }我们在测试方法中生成User类对象,但是只给username赋值了。 因此这个User对象传入UserMapper.xml的select标签后,是根据username判断查询出的记录。 另外需要注意的是,如果没有otherwise,当choose标签前面的when条件都不满足时,此时SQL语句就会变成select * from user where 1=1 那么就会把数据库中的所有记录查出来,但是我们的返回结果接收是User单个对象,因此会导致报错。 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |